import * as types from '../mutation-types.js'
import apiurl from '@/api/apiurl'

//全局状态
const state = {
  mapParams: '',
  assetsList: [],
  assetsMapLists: [],
  changeAssets: {
    address: ''
  },
  showName: true,
  assetsDetails: [],
  exportData: []
}

//getters
const getters = {
  mapParams: state => {
    return state.mapParams
  },
}

const mutations = {
  [types.SET_MAPPARAMS] (state, mapParams) {
    state.mapParams = mapParams
  },

  setMapParams(state, mapParams) {
    state.mapParams = mapParams
  },

  [types.SET_AESSETSLIST] (state, assetsList) {
    state.assetsList = assetsList
  },

  [types.SET_AESSETSMAPLISTS] (state, assetsMapLists) {
    let arr = state.assetsMapLists
    if(!(arr.find(item => item.facilityNum === assetsMapLists.facilityNum))) {
      arr.push(assetsMapLists)
      state.assetsMapLists = arr
    }
  },

  [types.SET_EMPTY] (state) {
    state.assetsMapLists = []
  },

  [types.SET_CHANGEASSETS] (state, params) {
    // console.log(params)
    if(params.info || params.info===0) {
      let arr = state.assetsMapLists
      console.log(arr)
      state.changeAssets = { ...arr[params.info], ...params }
    } else {
      state.changeAssets = { address: '' }
    }
  },

  [types.SET_SHOWNAME] (state, flag) {
    state.showName = flag
  },

  [types.SET_RESETASSETS] (state, isObj) {
    let arr = state.assetsDetails
    let keys = Object.keys(isObj)
    if(keys.length > 0) {
      if(!(arr.find(item => item.facilityName === isObj.facilityName))) {
        arr.push(isObj)
        state.assetsDetails = arr
      }
    } else {
      state.assetsDetails = []
    }
  },

  [types.SET_EXPORTDATA] (state, arr) {
    state.exportData = arr
  }
}

const actions = {

  /**
   * 获取案件详情
   * @param params{ itemNo: '案件编号' }
   * @returns { status: 1, result: { gpsFacilityList: [{}], gpsDishList: [{}], gpsLawCaseInfo: {} }, message: '請求成功' }
   */
  async fetchLawCaseInfo({ state, commit, dispatch }, params = {}) {
    // console.log(params,'获取案件详情参数')
    // console.log(apiurl.lawCaseInfo)
    let res = {}
    if(params.itemNo) {
      commit(types.SET_MAPPARAMS, params.itemNo)
      res = await Api.request(apiurl.lawCaseInfo, {
        ...params
      })
    } else {
      res = await Api.request(apiurl.lawCaseInfo, {
        itemNo: state.mapParams
      })
    }
    if(res.result.gpsFacilityList.length) {
      //868120272085611
      commit(types.SET_AESSETSLIST, res.result.gpsFacilityList)
      let assetsLists = res.result.gpsFacilityList
      assetsLists.forEach(item => {
        dispatch('fetchFacilityInfo', { facilityNum: item.facilityNum })
        dispatch('fetchImmediatelyPosition', { facilityNum: item.facilityNum })
      })
    }
    return res
  },

  //使用设备编号获取设备信息
  async fetchFacilityInfo({ commit }, params = {}) {
    // console.log(params,'获取设备信息')
    // console.log(apiurl.facilityInfo)
    let res = await Api.request(apiurl.facilityInfo, {
      ...params
    })
    commit(types.SET_RESETASSETS, {
      ...res.result
    })

    return res
  },

  //使用设备编号 开始结束时间 获取设备轨迹
  async fetchTrackView({ commit }, params = {}) {
    // console.log(params,'获取设备轨迹')
    // console.log(apiurl.trackView)
    let res = await Api.request(apiurl.trackView, {
      ...params
    })
    commit(types.SET_EXPORTDATA, res.result.list)
    return res
  },

  //发送指令
  async fetchSendIns(_, params = {}) {
    console.log(params,'发送指令')
    console.log(apiurl.sendIns)
    let res = await Api.request(apiurl.sendIns, {
      ...params
    })
    return res
  },

  //获取设备实时定位
  async fetchImmediatelyPosition({ commit, state }, params = {}) { //facilityNum   设备iemi
    // console.log(params,'获取设备实时定位')
    // console.log(apiurl.immediatelyPosition)
    let res = await Api.request(apiurl.immediatelyPosition, {
      ...params
    })
    commit(types.SET_AESSETSMAPLISTS, res.result)
    return res
  },

  //清除list
  emptyList({ commit }) {
    commit(types.SET_EMPTY)
  },

  //选中要显示的设备信息
  changeAssets({ commit, state }, params = { info: '' }) {
    commit(types.SET_CHANGEASSETS, { ...params })
  },

  //显示设备名
  changeShowName({ commit }, params = true) {
    commit(types.SET_SHOWNAME, params)
  },

  resetAssetsList({ commit }, params = {}) {
    commit(types.SET_RESETASSETS, {
      ...params
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
